package com.nit.lab.vote.controller.vote;

import com.nit.lab.vote.dto.utils.OpResultDTO;
import com.nit.lab.vote.dto.utils.PasswordDTO;
import com.nit.lab.vote.dto.utils.TableReqDTO;
import com.nit.lab.vote.dto.utils.TableRspDTO;
import com.nit.lab.vote.dto.vote.VoteDTO;
import com.nit.lab.vote.service.vote.CandidateService;
import com.nit.lab.vote.service.vote.VoteService;
import com.nit.lab.vote.utils.ExportUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/api/vote")
public class VoteController {
    @Resource
    private VoteService voteService;
    private final Logger logger = LoggerFactory.getLogger(ProjectController.class);

    @RequestMapping(value = "/logVote", method = RequestMethod.POST)
    TableRspDTO logVote(@RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            tableRspDTO = voteService.logVote(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    @RequestMapping(value = "/listAll", method = RequestMethod.POST)
    TableRspDTO listAll(@RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            tableRspDTO = voteService.listAll(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    @RequestMapping(value = "/addVote", method = RequestMethod.POST)
    OpResultDTO add(@RequestHeader(name = "Access-Token") String token, @RequestBody List<VoteDTO> voteDTOS) {
        OpResultDTO op;
        try {
            op = voteService.addList(token, voteDTOS);
        } catch (Exception e) {
            op = new OpResultDTO();
            op.setMessage("error");
            op.setResult("投票失败");
            logger.error(e.toString());
        }
        return op;
    }

    @RequestMapping(value = "/ListVote", method = RequestMethod.POST)
    TableRspDTO listVote(@RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            tableRspDTO = voteService.listVote(tableReqDTO);
        } catch (Exception e) {
            tableRspDTO.setMessage("error");
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    @RequestMapping(value = "/queryVoted", method = RequestMethod.POST)
    TableRspDTO queryVoted(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            tableRspDTO = voteService.queryVoted(token,tableReqDTO);
        } catch (Exception e) {
            tableRspDTO.setMessage("error");
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    @RequestMapping(value = "/queryVoteResult", method = RequestMethod.POST)
    TableRspDTO queryVoteResult(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            tableRspDTO = voteService.queryVoteResult(token,tableReqDTO);
        } catch (Exception e) {
            tableRspDTO.setMessage("error");
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    @RequestMapping(value = "/download", method = RequestMethod.POST)
    public void voteExcel(HttpServletResponse response, @RequestBody TableReqDTO tableReqDTO) throws Exception {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String filename = "demo.xls";
        response.setHeader("Content-disposition", "attachment;filename=" + filename);
        List<List<String>> dataList = new ArrayList<>();
        List<VoteDTO> voteDTOS = voteService.voteExcel(tableReqDTO);
        ArrayList<String> list;
        String[] typeStrings = {"选择评价型", "同意反对型"};
        String[] agreeStrings = {"同意", "不同意", "弃权"};
        for (int i = 0; i < voteDTOS.size(); i++) {
            list = new ArrayList<>();
            VoteDTO voteDTO = voteDTOS.get(i);
            list.add(String.valueOf(i + 1));
            list.add(voteDTO.getProjectName());
            list.add(typeStrings[voteDTO.getProjectType()]);
            list.add(voteDTO.getCandidateName());
            list.add(voteDTO.getSubLevel());
            list.add(voteDTO.getWorkLevel());
            Integer agree = voteDTO.getAgree();
            if (agree != null) {
                list.add(agreeStrings[agree]);
            } else {
                list.add(null);
            }
            dataList.add(list);
        }
        List<String> headList = new ArrayList<>();
        headList.add("序号");
        headList.add("项目名称");
        headList.add("投票类型");
        headList.add("候选人姓名");
        headList.add("研究工作水平");
        headList.add("专业技术水平");
        headList.add("是否同意");
        byte[] bytes = ExportUtil.exportExcel(headList, dataList, "xls");

        response.getOutputStream().write(bytes);
    }
}
